﻿<?xml version="1.0" encoding="utf-8" ?>
<content>
	<mainContent>
		<![CDATA[
		<h2>DNAideXmlSiteMapProvider</h2>
			
		<p>A number of key extensions to the default XmlSiteMapProvider:</p>
		<ul>
			<li>Simple "hook in" to the DNAide UrlRewriter</li>
			<li>You can now set the value of the "key" attrbute (not available with the XmlSiteMapProvider)</li>
			<li>Virtual method "BuildSiteMapFile" - a simple way to generate dynamic sitemaps (see "More Advanced Example" below).</li>
		</ul>
		
		<h3>Web.Config Configuration</h3>
		<pre class="csharpcode">
&lt;system.web&gt;
    &lt;siteMap enabled=<span class="str">"true"</span> defaultProvider=<span class="str">"DNAideXmlSiteMapProvider"</span>&gt;
        &lt;providers&gt;
            &lt;clear/&gt;
            &lt;add name=<span class="str">"DNAideXmlSiteMapProvider"</span> type=<span class="str">"DNAide.Web.DNAideXmlSiteMapProvider"</span> siteMapFile=<span class="str">"Web.sitemap"</span> /&gt;
        &lt;/providers&gt;
    &lt;/siteMap&gt;
&lt;/system.web&gt;</pre>

		<h3>Simple SiteMap Example.</h3>
		<pre class="csharpcode">
&lt;?xml version=<span class="str">"1.0"</span> encoding=<span class="str">"utf-8"</span> ?&gt;
&lt;siteMap xmlns=<span class="str">"http://schemas.microsoft.com/AspNet/SiteMap-File-1.0"</span> &gt;
    &lt;siteMapNode key=<span class="str">"home"</span> url=<span class="str">"~/index.htm"</span> title=<span class="str">"Home"</span> rewriteToUrl=<span class="str">"~/_Templates/WebForms/Home.aspx"</span>&gt;    
        &lt;siteMapNode key=<span class="str">"news-list"</span> url=<span class="str">"~/news/index.htm"</span> title=<span class="str">"Latest News"</span> rewriteToUrl=<span class="str">"~/_Templates/WebForms/NewsList.aspx"</span>&gt;
            &lt;siteMapNode key=<span class="str">"news-1"</span> url=<span class="str">"~/news/dnaide-released.htm"</span> rewriteToUrl=<span class="str">"~/_Templates/WebForms/NewsView.aspx"</span> /&gt;
            &lt;siteMapNode key=<span class="str">"news-2"</span> url=<span class="str">"~/news/another-story.htm"</span> rewriteToUrl=<span class="str">"~/_Templates/WebForms/NewsView.aspx"</span> /&gt;
            &lt;siteMapNode key=<span class="str">"news-3"</span> url=<span class="str">"~/news/more-news.htm"</span> rewriteToUrl=<span class="str">"~/_Templates/WebForms/NewsView.aspx"</span> /&gt;
            &lt;siteMapNode key=<span class="str">"news-4"</span> url=<span class="str">"~/news/oldest-news.htm"</span> rewriteToUrl=<span class="str">"~/_Templates/WebForms/NewsView.aspx"</span> /&gt;
        &lt;/siteMapNode&gt;
    &lt;/siteMapNode&gt;
&lt;/siteMap&gt;</pre>

		<h3>DNAide Url Rewriter Attributes:</h3>
		<ul>
			<li><strong>rewriteToUrl: </strong> rewrite to another Url</li>	
			<li><strong>redirectToSiteMapNodeKey: </strong> redirect to another node in the sitemap</li>	
			<li><strong>redirectStatusCode: </strong> i.e. what is added to the response header e.g. 301, 404 etc. Default = "301"</li>
		</ul>
			
		<h3>Building Simple Dynamic Site Map Files.</h3>
		<p>The DNAideXmlSiteMapProvider class exposes a virtual "BuildSiteMapFile" method which is fired by the provider if the associated SiteMap file cannot be found.</p>
		<p>In the example below, the website uses two SiteMap files: Web.SiteMap and News.SiteMap. Web.SiteMap includes News.SiteMap. The News.SiteMap file is generated from a database table.</p>
		
		<p><strong>Step 1.</strong> Configure the Web.Config file.</p>
		<pre class="csharpcode">
&lt;siteMap enabled=<span class="str">"true"</span> defaultProvider=<span class="str">"Main"</span>&gt;
    &lt;providers&gt;
        &lt;clear/&gt;
        &lt;add name=<span class="str">"Main"</span> type=<span class="str">"DNAide.Web.DNAideXmlSiteMapProvider"</span> siteMapFile=<span class="str">"Web.sitemap"</span>/&gt;
        &lt;add name=<span class="str">"News"</span> type=<span class="str">"NewsXmlSiteMapProvider"</span> siteMapFile=<span class="str">"News.sitemap"</span>/&gt;
    &lt;/providers&gt;
&lt;/siteMap&gt;</pre>

		<p><strong>Step 2.</strong> Build Web.Sitemap file.</p>
		<pre class="csharpcode">
&lt;?xml version=<span class="str">"1.0"</span> encoding=<span class="str">"utf-8"</span> ?&gt;
&lt;siteMap xmlns=<span class="str">"http://schemas.microsoft.com/AspNet/SiteMap-File-1.0"</span> &gt;
    &lt;siteMapNode key=<span class="str">"home"</span> url=<span class="str">"~/index.htm"</span> title=<span class="str">"Home"</span> rewriteToUrl=<span class="str">"~/_Templates/WebForms/Home.aspx"</span>&gt;    
        &lt;siteMapNode provider=<span class="str">"News"</span> /&gt;
    &lt;/siteMapNode&gt;
&lt;/siteMap&gt;</pre>
		
		<p><strong>Step 3.</strong> Create a NewsXmlSiteMapProvider class inheriting from DNAideXmlSiteMapProvider and override the "BuildSiteMapFile" to read stories from the database and generate a relevant sitemap file.</p>
		
		<pre class="csharpcode">
<span class="kwrd">public</span> <span class="kwrd">class</span> NewsXmlSiteMapProvider : DNAideXmlSiteMapProvider
{
    <span class="kwrd">public</span> <span class="kwrd">override</span> <span class="kwrd">void</span> BuildSiteMapFile()
    {
        XmlWriterSettings settings = <span class="kwrd">new</span> XmlWriterSettings();
        settings.Indent = <span class="kwrd">true</span>;
        settings.IndentChars = <span class="str">"\t"</span>;
        settings.ConformanceLevel = ConformanceLevel.Document;
 
        <span class="kwrd">using</span> (XmlWriter writer = XmlWriter.Create(SiteMapPath, settings))
        {
            writer.WriteStartElement(<span class="str">"siteMap"</span>, <span class="str">"http://schemas.microsoft.com/AspNet/SiteMap-File-1.0"</span>);
            writer.WriteStartElement(<span class="str">"siteMapNode"</span>);
            writer.WriteAttributeString(<span class="str">"key"</span>, <span class="str">"news-list"</span>);
            writer.WriteAttributeString(<span class="str">"url"</span>, <span class="str">"~/news/index.htm"</span>);
            writer.WriteAttributeString(<span class="str">"title"</span>, <span class="str">"Latest News"</span>);
            writer.WriteAttributeString(<span class="str">"rewriteToUrl"</span>, <span class="str">"~/_Templates/WebForms/NewsList.aspx"</span>);
            
            <span class="rem">// Retrieve news stories from the database (or whereever) and write out SiteMapNodes</span>
 
            writer.WriteEndElement(); <span class="rem">// Close news-list</span>
            writer.WriteEndElement(); <span class="rem">// Close siteMap</span>
            writer.Flush();
        }
    }
}</pre>

		<p>Using the above example is a simple way to generate dynamic sitemaps. You have full control of the source for your sitemap i.e. you are not tied to SQLServer etc. To get the provider to rebuild the sitemap file, simply remove it from your website e.g. if an administrator adds / updates / or removes a news story, delete the News.sitemap file and the file will then be recreated.</p>
		
		]]>
	</mainContent>

	<secondaryContent>
		<![CDATA[
		]]>
	</secondaryContent>
</content>